package com.jlnk.newdemo.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.jlnk.newdemo.R;
import com.jlnk.newdemo.adapter.NewsAdapter;
import com.jlnk.newdemo.entity.NewsEntity;
import com.jlnk.newdemo.presenter.NewsPresenter;

import java.util.List;

public class NewsListActivity extends AppCompatActivity implements NewsView {
    private NewsPresenter presenter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_list);
        listView = findViewById(R.id.news_list_view);
        presenter = new NewsPresenter(this);

        presenter.loadNewsList("推荐");
    }

    @Override
    public void showNewsList(List<NewsEntity> newsList) {
        if (newsList.isEmpty()) {
            Toast.makeText(this, "没有新闻数据", Toast.LENGTH_SHORT).show();
            return;
        }
        NewsAdapter adapter = new NewsAdapter(this, newsList);
        listView.setAdapter(adapter);
    }

    @Override
    public void showNewsDetail(NewsEntity newsItem) {
        Intent intent = new Intent(this, NewsDetailActivity.class);
        intent.putExtra("NEWS_ITEM_URL", newsItem.getUrl());
        startActivity(intent);
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}